package com.ruoyi.channel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.channel.domain.TChannelDataStaff;
import com.ruoyi.channel.domain.bo.TChannelDataStaffBo;
import com.ruoyi.channel.domain.vo.TChannelDataStaffVo;
import com.ruoyi.channel.mapper.TChannelDataStaffMapper;
import com.ruoyi.channel.service.ITChannelDataStaffService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 渠道数据管理员业务员配置Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-01
 */
@RequiredArgsConstructor
@Service
public class TChannelDataStaffServiceImpl implements ITChannelDataStaffService {

    private final TChannelDataStaffMapper baseMapper;

    /**
     * 查询渠道数据管理员业务员配置
     */
    @Override
    public TChannelDataStaffVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询渠道数据管理员业务员配置列表
     */
    @Override
    public TableDataInfo<TChannelDataStaffVo> queryPageList(TChannelDataStaffBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TChannelDataStaff> lqw = buildQueryWrapper(bo);
        Page<TChannelDataStaffVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询渠道数据管理员业务员配置列表
     */
    @Override
    public List<TChannelDataStaffVo> queryList(TChannelDataStaffBo bo) {
        LambdaQueryWrapper<TChannelDataStaff> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TChannelDataStaff> buildQueryWrapper(TChannelDataStaffBo bo) {
        LambdaQueryWrapper<TChannelDataStaff> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getChannelDataId() != null, TChannelDataStaff::getChannelDataId, bo.getChannelDataId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TChannelDataStaff::getType, bo.getType());
        lqw.eq(bo.getUserId() != null, TChannelDataStaff::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getDataInfo()), TChannelDataStaff::getDataInfo, bo.getDataInfo());
        lqw.eq(bo.getCreateId() != null, TChannelDataStaff::getCreateId, bo.getCreateId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TChannelDataStaff::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增渠道数据管理员业务员配置
     */
    @Override
    public Boolean insertByBo(TChannelDataStaffBo bo) {
        TChannelDataStaff add = BeanUtil.toBean(bo, TChannelDataStaff.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改渠道数据管理员业务员配置
     */
    @Override
    public Boolean updateByBo(TChannelDataStaffBo bo) {
        TChannelDataStaff update = BeanUtil.toBean(bo, TChannelDataStaff.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TChannelDataStaff entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除渠道数据管理员业务员配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<TChannelDataStaffVo> staffVo(String type, Long channelDataId) {
        return baseMapper.staffVo(type, channelDataId);
    }

    @Override
    public void saveStaffInfo(Long channelDataId, String type, List<TChannelDataStaffBo> userList) {
        // 1、管理员
        baseMapper.delete(new LambdaQueryWrapper<TChannelDataStaff>().eq(TChannelDataStaff::getType, type).eq(TChannelDataStaff::getChannelDataId, channelDataId));

        if (!CollectionUtils.isEmpty(userList)) {
            List<TChannelDataStaff> dataStaffList = userList.stream().map(it -> {
                TChannelDataStaff dataStaff = new TChannelDataStaff();
                dataStaff.setType(type);
                dataStaff.setChannelDataId(channelDataId);
                dataStaff.setUserId(it.getUserId());
                // 业务员
                if (Objects.equals("2", type)) {
                    Map<String, String> map = new HashMap<>();
                    map.put("number", it.getNumber());
                    // 默认值为天
                    map.put("disType", !StringUtils.isNotBlank(it.getDisType()) ? "day" : it.getDisType());
                    dataStaff.setDataInfo(JsonUtils.toJsonString(map));
                }
                return dataStaff;
            }).collect(Collectors.toList());
            baseMapper.insertBatch(dataStaffList);
        }
    }
}
